package com.dzsw.boot.controller;

import com.dzsw.boot.protocol.Person;
import com.dzsw.boot.service.IBizService;
import com.dzsw.boot.util.MailUtil;
import jakarta.mail.*;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.PropertyAccessorFactory;
import org.springframework.web.bind.ServletRequestParameterPropertyValues;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Properties;

@RestController
@RequestMapping("/sc")
public class SampleController {

    private IBizService bizService;

    public SampleController(IBizService bizService) {
        this.bizService = bizService;
    }

    @GetMapping("/s1")
    public String sample1() {
        // ServletRequestParameterPropertyValues
        return "ok";
    }

    @GetMapping("/s2")
    public Object sample2(HttpServletRequest request) {
        ServletRequestParameterPropertyValues propertyValues = new ServletRequestParameterPropertyValues(request);
        Person person = new Person();
        BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(person);
        wrapper.setPropertyValues(propertyValues);
        return person;
    }

    @GetMapping("/s3")
    public String sample3() {
        bizService.firstBiz();
        return "ok";
    }

    @GetMapping("/s4")
    public String sample4() {
        // 配置 SMTP 服务器信息
        String smtpHost = "smtp.exmail.qq.com"; // 例如 Gmail SMTP 服务器
        String smtpPort = "587"; // TLS 默认端口
        String username = "sunzhel@sy-binal.com"; // 你的邮箱地址
        String password = "LmUmTJ7bjZdbjRXy"; // 你的邮箱密码
        // 设置属性
        Properties props = new Properties();
        props.put("mail.smtp.host", smtpHost); // SMTP 服务器地址
        props.put("mail.smtp.port", smtpPort); // SMTP 端口
        props.put("mail.smtp.auth", "true"); // 启用身份验证
        props.put("mail.smtp.starttls.enable", "true"); // 启用 TLS
        props.put("mail.smtp.ssl.trust", smtpHost); // 信任的 SMTP 服务器
        try {
            // 创建会话
            Session session = Session.getInstance(props, new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });
            // 创建邮件
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("sunzhe@sy-binal.com")); // 发件人地址
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("sunzhe@sy-binal.com")); // 收件人地址
            message.setSubject("Test Jakarta Mail TLS Email"); // 邮件主题
            message.setText("This is a test email sent using Jakarta Mail with TLS encryption."); // 邮件正文

            // 发送邮件
            Transport.send(message);
            System.out.println("邮件发送成功！");

        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return "ok";
    }
}
